//
//  RoomCategoryViewLayout.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/18.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "RoomCategoryViewLayout.h"


static const CGFloat item_row_margin = 20;
static const CGFloat item_height = 135;
static const CGFloat item_column_margin = 10;

@interface RoomCategoryViewLayout()

//属性 item数组
@property(nonatomic,strong)NSMutableArray *layoutAttributeArray;
@property(nonatomic,assign)BOOL cColumn ;

@end
@implementation RoomCategoryViewLayout

- (void)prepareLayout{
    CGFloat layoutWidth = self.collectionView.bounds.size.width - self.sectionInset.left - self.sectionInset.right;
    
//    CGFloat x = self.minimumInteritemSpacing;
    CGFloat itemWidth = (layoutWidth - item_row_margin*(self.columnCount + 1))/self.columnCount;
    
    [self calculateAttributesWithItemWidth:itemWidth];
}


- (void)calculateAttributesWithItemWidth:(CGFloat)itemWidth{
    
    //每一列的总高度
    CGFloat heightColumn[self.columnCount];
    //每一列的总个数
    CGFloat itemCountColumn[self.columnCount];
    
    
    for (int index = 0;index < _columnCount; index ++){
        CGFloat top = self.sectionInset.top;
        heightColumn[index] = top;
        itemCountColumn[index] = 0;
    }
    
    //遍历
    NSInteger index = 0;
    NSMutableArray *attributeArray = [[NSMutableArray alloc]initWithCapacity:self.roomArray.count];
    
    for(LERoomItem *item in self.roomArray){
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        //找出最短的那一列
        NSInteger minColumn = [self findoutMinumnColumn:heightColumn];
        itemCountColumn[minColumn]++;
        
        CGFloat itemX = self.sectionInset.left + item_row_margin + (itemWidth +item_row_margin ) * minColumn;
        //找出最短那一列的高度
        CGFloat itemY = heightColumn[minColumn];
//        BOOL cColumn = NO;
//        if (itemY - 0.0001 < 0.0f) {
//            if (cColumn) {
//                itemY = item_column_margin*3;
//            }else{
//                itemY = item_column_margin;
//            }
//            cColumn = !cColumn;
//        }
        CGFloat margin = 0;

        if (_cColumn) {
            margin = arc4random() % 20 +5;
        }else{
            margin = arc4random() % 30 +1;
        }
        _cColumn = !_cColumn;
    
        
        CGFloat itemH =   item_height;//+arc4random() % 50;//item_height+random()%(index+10);//(random()%20+item_height) * itemWidth / (random()%8+item_height); //random()%(index*3+5)+1+item_height;
        //更新最短列的高
        heightColumn[minColumn] = item_column_margin + itemH + itemY + margin;
        attribute.frame = CGRectMake(itemX, itemY+margin, itemWidth, itemH);
        
        
        [attributeArray addObject:attribute];
        index ++;
    }
    //找出最高的那一列 的列号
    NSInteger maxColumn = [self findoutMaxColumn:heightColumn];
    CGFloat itemHeightt = (heightColumn[maxColumn] - item_column_margin * (itemCountColumn[maxColumn]+1) )/itemCountColumn[maxColumn];
//    CGFloat itemHeightt = heightColumn[maxColumn] / itemCountColumn[maxColumn];
    
    self.itemSize = CGSizeMake(itemWidth, itemHeightt);
    
    /**页脚*/
//    NSIndexPath *footerIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//    UICollectionViewLayoutAttributes *footerAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:footerIndexPath];
//    footerAttributes.frame = CGRectMake(0, heightColumn[maxColumn], self.collectionView.bounds.size.width, 50);
//    
//    [attributeArray addObject:footerAttributes];
    self.layoutAttributeArray = [attributeArray copy];
    
    
    /**返回 contentSize*/
//    self.collectionView.contentSize = CGSizeMake(self.collectionView.frame.size.width, heightColumn[maxColumn]);
    
    
}

- (NSInteger)findoutMinumnColumn:(CGFloat *)heightColumn{
    NSInteger result = 0;
    CGFloat max = CGFLOAT_MAX;
    for (int i = 0; i < self.columnCount; i++) {
        if (heightColumn[i] < max) {
            max = heightColumn[i];
            result = i;
        }
    }
    
    return result;
}


- (NSInteger)findoutMaxColumn:(CGFloat *)heightColumn{
    NSInteger result = 0;
    CGFloat min = -1;
    for (int i = 0; i < self.columnCount; i++) {
        if (heightColumn[i] > min) {
            min = heightColumn[i];
            result = i;
        }
    }
    
    return result;
}



/**
 *  跟踪效果：当到达要显示的区域时 会计算所有显示item的属性
 *           一旦计算完成 所有的属性会被缓存 不会再次计算
 *  @return 返回布局属性(UICollectionViewLayoutAttributes)数组
 */
- (NSArray <UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    // 直接返回计算好的布局属性数组
    return self.layoutAttributeArray;
}

- (NSMutableArray *)layoutAttributeArray{
    if (!_layoutAttributeArray) {
        _layoutAttributeArray = [NSMutableArray array];
    }
    return _layoutAttributeArray;
}

@end
